<?php

use Illuminate\Database\Seeder;

class PermissionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $data = json_decode(self::getJsonData(), 1);

        if (!empty($data['data']))
        {
            DB::statement('SET FOREIGN_KEY_CHECKS=0;');
            Permissions::truncate();
            DB::statement('SET FOREIGN_KEY_CHECKS=1;');

            foreach ($data['data'] as $key=>$val)
            {
                $model = new Permissions;
                $model->id = $val['id'];
                $model->name = $val['name'];
                $model->slug = $val['slug'];
                $model->http_method = $val['http_method'];
                $model->http_path = $val['http_path'];
                $model->save();
            }
        }
    }

    public static function getJsonData()
    {
        return '
            {
                "data":
                [
                    {
                        "id": 1,
                        "name": "所有权限",
                        "slug": "*",
                        "http_method": "",
                        "http_path": "*",
                        "created_at": null,
                        "updated_at": null
                    },
                    {
                        "id": 2,
                        "name": "首页",
                        "slug": "dashboard",
                        "http_method": "GET",
                        "http_path": "/",
                        "created_at": null,
                        "updated_at": null
                    },
                    {
                        "id": 3,
                        "name": "登录/退出",
                        "slug": "auth.login",
                        "http_method": "",
                        "http_path": "/auth/login
            /auth/logout",
                        "created_at": null,
                        "updated_at": null
                    },
                    {
                        "id": 4,
                        "name": "用户设置",
                        "slug": "auth.setting",
                        "http_method": "GET,PUT",
                        "http_path": "/auth/setting",
                        "created_at": null,
                        "updated_at": null
                    },
                    {
                        "id": 5,
                        "name": "系统设置",
                        "slug": "auth.management",
                        "http_method": "",
                        "http_path": "/auth/roles
            /auth/permissions
            /auth/menu
            /auth/logs",
                        "created_at": null,
                        "updated_at": "2020-06-20 16:34:13"
                    },
                    {
                        "id": 6,
                        "name": "客户管理",
                        "slug": "member",
                        "http_method": "",
                        "http_path": "/member
            /member/score",
                        "created_at": "2020-06-20 11:48:03",
                        "updated_at": "2020-06-23 22:04:24"
                    },
                    {
                        "id": 8,
                        "name": "积分日志",
                        "slug": "member.score",
                        "http_method": "",
                        "http_path": "/member/score/log*",
                        "created_at": "2020-06-23 18:47:39",
                        "updated_at": "2020-06-23 18:50:05"
                    },
                    {
                        "id": 9,
                        "name": "品牌管理",
                        "slug": "goods.brand",
                        "http_method": "",
                        "http_path": "/goods/brand*",
                        "created_at": "2020-06-23 18:49:55",
                        "updated_at": "2020-06-23 18:49:55"
                    },
                    {
                        "id": 10,
                        "name": "产品管理",
                        "slug": "goods",
                        "http_method": "",
                        "http_path": "/goods/list*
            /goods/addGoodsAttr*
            /goods/addGoodsAttrValue*",
                        "created_at": "2020-06-23 21:51:31",
                        "updated_at": "2020-06-23 21:51:31"
                    },
                    {
                        "id": 11,
                        "name": "分类管理",
                        "slug": "goods.class",
                        "http_method": "",
                        "http_path": "/goods/class*",
                        "created_at": "2020-06-23 21:52:06",
                        "updated_at": "2020-06-23 21:52:06"
                    },
                    {
                        "id": 12,
                        "name": "订单管理",
                        "slug": "order",
                        "http_method": "",
                        "http_path": "/orders*
            /order/invalid*
            /order/delivery/{id?}",
                        "created_at": "2020-06-23 21:52:57",
                        "updated_at": "2020-06-23 21:52:57"
                    },
                    {
                        "id": 13,
                        "name": "订单日志",
                        "slug": "order.log",
                        "http_method": "",
                        "http_path": "/order/log*",
                        "created_at": "2020-06-23 21:53:23",
                        "updated_at": "2020-06-23 21:53:23"
                    },
                    {
                        "id": 14,
                        "name": "活动管理",
                        "slug": "activity",
                        "http_method": "",
                        "http_path": "/activity/list*",
                        "created_at": "2020-06-23 21:54:32",
                        "updated_at": "2020-06-23 21:54:52"
                    },
                    {
                        "id": 15,
                        "name": "活动签到",
                        "slug": "activity.enter",
                        "http_method": "",
                        "http_path": "/activity/enter*",
                        "created_at": "2020-06-23 21:55:19",
                        "updated_at": "2020-06-23 21:55:19"
                    },
                    {
                        "id": 16,
                        "name": "社区管理",
                        "slug": "community",
                        "http_method": "",
                        "http_path": "/community*",
                        "created_at": "2020-06-23 21:55:44",
                        "updated_at": "2020-06-23 21:55:44"
                    },
                    {
                        "id": 17,
                        "name": "基本设置",
                        "slug": "setting",
                        "http_method": "",
                        "http_path": "/setting*
            /setting/save*
            /upload*",
                        "created_at": "2020-06-23 21:57:56",
                        "updated_at": "2020-06-23 21:57:56"
                    },
                    {
                        "id": 18,
                        "name": "轮播图",
                        "slug": "setting.slideshow",
                        "http_method": "",
                        "http_path": "/slideshow",
                        "created_at": "2020-06-23 21:58:16",
                        "updated_at": "2020-06-23 21:58:16"
                    }
                ]
            }
        ';
    }
}
